import Vue from 'vue'
import VueRouter from 'vue-router'

import Cookies from 'js-cookie'

Vue.use(VueRouter)

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  /* {
    path: '/',
    component: () => import('@/views/Home.vue'),
    children: [
      {
        path: '/home',
        component: () => import('@/views/Chart/Chart.vue')
      },
      {
        path: '/user',
        component: () => import('@/views/UserManage/UserManage.vue')
      },
      {
        path: '/video',
        component: () => import('@/views/VideoManger/VideoManger.vue')
      },
      {
        path: '/',
        redirect: '/home'
      }
    ],
  }, */
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue')
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/NotFound/NotFound.vue'),
  },
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // 如果用户访问的登录页，直接放行
  if (to.path === '/login') return next()
  // 如果用户访问的后台管理页面，需要先判断是否有token
  const tokenStr = Cookies.get('token_key')
  if (!tokenStr) return next('/login')
  next()
})

export default router
